/**********************************************************************
 *
 *  MODULE: MPADWIN.H
 *  AUTHOR: BRIAN M. WORKMAN
 *  START DATE: November 21, 2005, 5:49:52 PM
 *
 **********************************************************************/

#ifndef EDITORWIN_H
#define EDITORWIN_H

#define FILE_ASSOC "MUDpad File (*.mud *.txt)\nAll Files (*.*)"
#define RELOAD_TIMER 2 * 1000

#include <QtGui>
#include <QTimer>
#include "../gdoc/gnumberededit.h"
#include "../gdoc/devlinenumber.h"
#include "../gdoc/gdoc.h"
#include "find.h"
#include "findreplace.h"
#include "findbar.h"


struct DocProps {
    bool new_data;
    QString path;
    bool copy_available;
    bool undo_available;
    bool redo_available;
    QString file_name;
    QString full_file_name;
    QDateTime last_modified;
};

class MPadWin : public QMainWindow
{
    Q_OBJECT
public:
    /* INTERFACE */
    MPadWin(QWidget *parent = 0, Qt::WFlags f = 0 );
    void prepareObjects();
    QString lastPath() const;
    GNumberedEdit *curTab() { return qobject_cast<GNumberedEdit*> (tabs->currentWidget()); }
    GDoc *curDoc() { return curTab()->edit(); }

protected:
    QTabWidget *tabs;
    QStackedWidget *stack;
    QIcon *iconOk, *iconNo, *iconFlag;
    void closeEvent(QCloseEvent *event);
    virtual void resizeEvent ( QResizeEvent * event );
    virtual void moveEvent ( QMoveEvent * event );

public slots:
    void fontUpdateSize(int);
    void fontUpdate(QFont);
    void slotRequestFindPrevious(bool y);
    void slotRequestFindNext(bool y);
    void findBarHidden(bool);
    void checkDocumentChanges();
    void newFile();
    void updateStatus(GNumberedEdit * = 0);
    void open();
    void save();
    void saveAs();
    void doSaveAll();
    bool closeFile(GNumberedEdit* v = 0);
    void about();
    void doPaste();
    void switchDocument(int);
    void doCut();
    void doCopy();
    void showHideToolbars();
    void doPrint();
    void doPDFPrint();
    void setLastPath(QString path);
    void enableCopy(bool y);
    void enableRedo(bool y);
    void enableUndo(bool y);
    void tabLeft();
    void tabRight();
    void setMudFriendly(bool yn);
    void setViewLines(bool);
    void updateLeft(int);
    void updateRight(int);
    void updateDot(int);
    void doFind();
    bool doFindNext();
    bool doFindPrevious();
    void doFindReplace();
    void doReplace();
    void doReplaceAll();
    bool doCloseAll();
    void doReload(GNumberedEdit *v = 0);
    void doUndo();
    void doRedo();
    void doFindB(QString s, bool forward = true, bool matchcase = false);

private:
    /* WIDGETS */
    QSpinBox *edRight;
    QSpinBox *edLeft;
    QCheckBox *chkPreserveDot;
    FindBar *fbar;

    /* ACTIONS */
    QAction *actFindReplace;
    QAction *actFindNext;
    QAction *actFindPrevious;
    QAction *actFind;
    QAction *actMudFriendly;
    QAction *newAct;
    QAction *newTabAct;
    QAction *removeTabAct;
    QAction *openAct;
    QAction *saveAct;
    QAction *saveAsAct;
    QAction *saveAllAct;
    QAction *closeAct;
    QAction *exitAct;
    QAction *cutAct;
    QAction *copyAct;
    QAction *pasteAct;
    QAction *aboutAct;
    QAction *aboutQtAct;
    QAction *toggleToolbarsAct;
    QAction *getFontAct;
    QAction *printAct;
    QAction *printPDFAct;
    QAction *actTabRight;
    QAction *actTabLeft;
    QAction *actContextHelp;
    QAction *actViewLines;
    QAction *actCloseAll;
    QAction *actReload;
    QAction *actUndo;
    QAction *actRedo;
    
    /* TOOLBARS/BUTTONS/MENUS */
    QToolBar *mainToolBar;
    QToolBar *mudToolBar;
    QToolBar *fontToolBar;
    QToolButton *newTabButton;
    QToolButton *removeTabButton;
    QMenu *fileMenu;
    QMenu *editMenu;
    QMenu *viewMenu;
    QMenu *helpMenu;
    QFontComboBox *fonts;
    QSpinBox *fontSize;

    /* METHODS */
    bool loadFile(GNumberedEdit *v, QString fileName);
    QString strippedName(const QString fullFileName);
    QString strippedPath(const QString fullFileName);
    bool viewLines() const;
    void openFiles(QStringList fileNames);
    void createActions();
    void createMenus();
    void createToolBars();
    void createStatusBar();
    void createWhatsThis();
    void readSettings();
    void writeSettings();
    void setupConnections();
    void saveDocument(GNumberedEdit *v = 0, bool promptFileName = false);
    void saveDocument(GNumberedEdit *v, QString fileName);
    void loadFile(const QString &fileName);
    bool saveFile(const QString &fileName);
    QString newFileName();
    void updateDisplay(GNumberedEdit *);
    void updateDisplay();
    void updateDisplays();
    void assignConnections(GNumberedEdit *);
    void initNewView (GNumberedEdit *);
    void makeWhatsThis(QAction *act, QString icon = 0, QString title = "",QString text = "<<insert text>>");
    void makeWhatsThis(QWidget *act, QString icon = 0, QString title = "",QString text = "<<insert text>>");
    QString title(GNumberedEdit *v);
    QString fileName(GNumberedEdit *v);
        
    /* DATA MEMBERS */
    QFont m_font;
    QHash<GNumberedEdit*, DocProps> docs;
    bool m_closing;
    bool m_mudfriendly;
    bool m_toolbars;
    bool m_initializing;
    bool m_preservedot;
    bool m_lines;
    QString m_replace;    
    QSize m_size;
    QPoint m_pos;
    int m_indent;
    int m_wrap;
    bool m_opening;
    bool m_dot;
    QString m_search;
    bool m_back;
    bool m_case;
    bool m_word;
    DlgFindReplace *replace;
    QTimer *m_reload_timer;
#ifndef Q_OS_X11
    bool m_maximized;
#endif
    QString m_lastpath;
    bool SHOWN;

signals:
    void dataSaved(GNumberedEdit*);   
    void textFound(bool);
};

#endif
